package com.salou.cellar.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.salou.cellar.R;
import com.salou.cellar.contentprovider.CellarContentProvider;
import com.salou.cellar.dto.Bottle;

public class ListBottlesActivity extends ListActivity implements
		OnItemClickListener, OnClickListener {
	private static final String SHOW_BOTTLE= "com.salou.cellar.showBottle";
	public static final int RESULT_ENABLE = 1;

	private CellarContentProvider ccp;
	private Bundle filters;
	private ListView mBottlesList;
	private Button mBackButton;
	private Cursor mCursor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ccp = new CellarContentProvider(getApplicationContext());
		setContentView(R.layout.list_of_bottles);
		this.filters = getIntent().getExtras().getBundle("FILTERS");
		initDisplay();
		showList();
	}

	private void initDisplay() {
		this.mBottlesList = getListView();
		this.mBottlesList.setOnItemClickListener(this);

		this.mBackButton = (Button) findViewById(R.id.back_button);
		this.mBackButton.setOnClickListener(this);
	}

	private void showList() {
		// List<Bottle> list = ccp.getBottles(filters);
		// BottleListAdapter bottlesView = new BottleListAdapter(this, list);
		// this.mBottlesList.setAdapter(bottlesView);

		mCursor = ccp.getBottlesCursor(filters);
		startManagingCursor(mCursor);

		// Now create a new list adapter bound to the cursor.
		// SimpleListAdapter is designed for binding to a Cursor.
		ListAdapter adapter = new BottlesAdapter(
				// Context.
				this, 
				// Specify the row template to use (here, two columns bound to the two retrieved cursor rows).
				R.layout.list_of_bottles_item, 
				// Pass in the cursor to bind to.
				mCursor, 
				// Array of cursor columns to bind to.
				new String[] { Bottle.COL_id, Bottle.COL_nomCru, Bottle.COL_label, Bottle.COL_millesime,
					Bottle.COL_country_label, Bottle.COL_subregion_label, Bottle.COL_area_label}, 
				// Parallel array of which template objects to bind to those columns.
				new int[] { R.id.id,  R.id.nom_cru, R.id.label, R.id.millesime,
						R.id.country_label, R.id.subregion_label,
						R.id.area_label });
		// Bind to our new adapter.
		setListAdapter(adapter);
	}

	@Override
	public void onClick(View v) {
		// back button clicked
		setResult(RESULT_OK);
		this.finish();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// a bottle has been chosen
		int id=((BottlesAdapter)getListAdapter()).getBottleId(arg1);
		Intent intent = new Intent(SHOW_BOTTLE);
		intent.putExtra(Bottle.COL_id, id);
		startActivityForResult(intent, RESULT_ENABLE);
	}

	/**
	 * Custom adapter used to display account icons and descriptions in the
	 * account spinner.
	 */
	private class BottleListAdapter extends ArrayAdapter<Bottle> {
		public BottleListAdapter(Context context, List<Bottle> bottles) {
			super(context, R.layout.list_of_bottles_item, bottles);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			// Inflate a view template
			if (convertView == null) {
				LayoutInflater layoutInflater = getLayoutInflater();
				convertView = layoutInflater.inflate(R.layout.list_of_bottles_item,
						parent, false);
			}

			TextView nomcru = (TextView) convertView.findViewById(R.id.nom_cru);
			TextView millesime = (TextView) convertView
					.findViewById(R.id.millesime);
			TextView subregion_label = (TextView) convertView
					.findViewById(R.id.subregion_label);
			TextView country_label = (TextView) convertView
					.findViewById(R.id.country_label);
			TextView area_label = (TextView) convertView
					.findViewById(R.id.area_label);
			TextView label = (TextView) convertView.findViewById(R.id.label);
			// TextView volume = (TextView) convertView
			// .findViewById(R.id.volume);

			// Populate template
			Bottle data = getItem(position);
			nomcru.setText(data.getNomCru());
			millesime.setText(data.getMillesime());
			subregion_label.setText(data.getSubregion_label());
			country_label.setText(data.getCountry_label());
			area_label.setText(data.getArea_label());
			label.setText(data.getLabel());

			return convertView;
		}
	}
	/**
	 * Custom adapter used to display account icons and descriptions in the
	 * account spinner.
	 */
	private class BottlesAdapter extends SimpleCursorAdapter {

		Map<View,Integer> viewToInt=new HashMap<View,Integer>();
		
		public BottlesAdapter(Context context, int layout, Cursor c,
				String[] from, int[] to) {
			super(context, layout, c, from, to);
		}

		public int getBottleId(View arg1) {
			return viewToInt.get(arg1);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = super.newView(context, cursor, parent);
			int bottleId=cursor.getInt(cursor.getColumnIndex("_id"));
			viewToInt.put(view, bottleId);
			return view;
		}
		
		
	}
}
